package org.example.library.controller;

import lombok.extern.slf4j.Slf4j;
import org.example.library.model.BookInfo;
import org.example.library.model.PageRequest;
import org.example.library.model.PageResult;
import org.example.library.model.Result;
import org.example.library.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

import static jdk.nashorn.internal.runtime.regexp.joni.Config.log;
@Slf4j
@RequestMapping("/book")
@RestController
public class BookController {
    @Autowired
    private BookService bookService;
    @RequestMapping("/getListByPage")
    public Result  getListByPage(PageRequest pageRequest) {
        if(pageRequest.getPageSize() < 0 || pageRequest.getCurrentPage() < 1) {
            return Result.fail("参数校验失败");
        }
        PageResult<BookInfo> bookInfoPageResult = null;
        try {
            bookInfoPageResult = bookService.getBookListByPage(pageRequest);
            return Result.success(bookInfoPageResult);
        } catch (Exception e) {
            log.error("查询信息错误，e:{}",e);
            return Result.fail(e.getMessage());
        }
    }

    @RequestMapping("/queryBookById")
    public Result queryBookById(Integer bookId){
        if (bookId==null || bookId<=0){
            return Result.unlogin();
        }
        BookInfo bookInfo = bookService.queryBookById(bookId);
        return Result.success(bookInfo);
    }
    @RequestMapping("/updateBook")
    public Result updateBook(BookInfo bookInfo) {
        log.info("修改图书:{}", bookInfo);
        try {
            bookService.updateBook(bookInfo);
            return Result.success("");
        } catch (Exception e) {
            log.error("修改图书失败", e);
            return Result.fail("修改失败");
        }
    }
    @RequestMapping("/addBook")
    public Result addBook(BookInfo bookInfo) {
        log.info("添加图书:{}", bookInfo);
        if (!StringUtils.hasLength(bookInfo.getBookName())
                || !StringUtils.hasLength(bookInfo.getAuthor())
                || bookInfo.getCount()==null
                || bookInfo.getPrice()==null
                || !StringUtils.hasLength(bookInfo.getPublish())
                || bookInfo.getStatus() ==null
        ) {
            return Result.fail("输⼊参数不合法, 请检查⼊参!");
        }
        try {
            bookService.addBook(bookInfo);
            return Result.success("");
        } catch (Exception e) {
            log.error("添加图书失败", e);
            return Result.fail(e.getMessage());
        }
    }

    @RequestMapping("/batchDeleteBook")
    public boolean batchDeleteBook(@RequestParam List<Integer> ids){
        log.info("批量删除图书, ids:{}",ids);
        try{
            bookService.batchDeleteBook(ids);
        }catch (Exception e){
            log.error("批量删除异常,e:",e);
            return false;
        }
        return true;
    }
}

